﻿using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;
using ReportingServices.DeploymentCenter.Core;
using ReportingServices.DeploymentCenter.Core.Logging;
using ReportingServices.DeploymentCenter.Core.ReportingServicesServiceReference;

namespace ReportingServices.DeploymentCenter.UI
{
    public partial class MainForm : BaseForm
    {
        #region Methods
        
        private void DeployItems()
        {
            Logger.LogInformation("Begin item deployment.");

            string deploymentFolder = Properties.Settings.Default.DeploymentFolder;

            if (String.IsNullOrWhiteSpace(deploymentFolder))
            {
                MessageBox.Show("You must select a deployment folder.");
                return;
            }

            ProgressBar.Value = 0;
            ProgressBar.Maximum = DeploymentItemsFileDialog.FileNames.Count();

            using (DeploymentManager deploymentManager = new DeploymentManager(ReportingServicesServiceConfiguration))
            {
                deploymentManager.OnItemDeployed += DeploymentManager_OnItemUploaded;

                deploymentManager.DeployFiles(deploymentFolder, DeploymentItemsFileDialog.FileNames, OverwriteItemsCheckBox.Checked);

                deploymentManager.OnItemDeployed -= DeploymentManager_OnItemUploaded;
            }

            Logger.LogInformation("Deployment complete.");
            MessageBox.Show("Deployment complete.");

            ItemsListBox.Items.Clear();
        }

        public void DeploymentManager_OnItemUploaded(object sender, EventArgs e)
        {
            if (ProgressBar.Value == ProgressBar.Maximum)
                return;

            if (e is ItemDeployedEventArgs)
            {
                ItemDeployedEventArgs arguments = e as ItemDeployedEventArgs;

                if (arguments.DeploymentResult != null && arguments.DeploymentResult.ItemInformation != null)
                    Logger.LogInformation(String.Format("{0} has successfully been deployed.", arguments.DeploymentResult.ItemInformation.Name));
                
                if (arguments.DeploymentResult != null && arguments.DeploymentResult.Warnings != null && arguments.DeploymentResult.Warnings.Length > 0)
                {
                    foreach (Warning warning in arguments.DeploymentResult.Warnings)
                    {
                        Logger.LogInformation(warning.Message);
                    }
                }
            }

 	        ProgressBar.Value++;
        }

        public MainForm()
        {
            InitializeComponent();
        }

        private void DisplaySelectedFiles()
        {
            foreach (string fileName in DeploymentItemsFileDialog.FileNames)
            {
                ItemsListBox.Items.Add(fileName);
            }
        }

        #endregion

        #region Event Handlers

        private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void DeployItemsButton_Click(object sender, EventArgs e)
        {
            try
            {
                DeployItems();
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
            }
        }

        private void DeploymentItemsFileDialog_FileOk(object sender, CancelEventArgs e)
        {
            DisplaySelectedFiles();
        }

        private void SelectItemsButton_Click(object sender, EventArgs e)
        {
            ItemsListBox.Items.Clear();
            DeploymentItemsFileDialog.ShowDialog();
        }

        private void SettingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SettingsForm settings = new SettingsForm();
            settings.Show(this);
        }

        #endregion
    }
}